package org.jiuling.aliyun.sms.convert;

import org.jiuling.aliyun.sms.model.QueryShortUrlResponseModel;
import org.jiuling.aliyun.sms.model.enums.ShortUrlStatusEnum;
import org.jiuling.basics.converter.DateConvert;
import org.jiuling.basics.enums.DateTimeFormatEnum;
import org.jiuling.basics.exception.BasicsException;
import org.jiuling.basics.util.EnumUtil;
import org.jiuling.basics.validation.ObjectVerify;
import org.jiuling.basics.validation.StringVerify;

/**
 * 查询短链接请求转换
 * @author 紫灵
 * @date 2023年02月21日 19时45分32秒
 */
public class QueryShortUrlResponseConvert {

    /** 私有构造方法，禁止创建对象 */
    private QueryShortUrlResponseConvert(){}

    /**
     * 内容转模型
     * @param queryShortUrlResponseBody 获取短链接响应内容
     * @return 获取短链接响应模型
     */
    public static QueryShortUrlResponseModel bodyToModel(com.aliyun.dysmsapi20170525.models.QueryShortUrlResponseBody queryShortUrlResponseBody){
        QueryShortUrlResponseModel queryShortUrlResponseModel = new QueryShortUrlResponseModel();
        queryShortUrlResponseModel.setCode(queryShortUrlResponseBody.getCode());
        queryShortUrlResponseModel.setMessage(queryShortUrlResponseBody.getMessage());
        queryShortUrlResponseModel.setRequestId(queryShortUrlResponseBody.getRequestId());

        com.aliyun.dysmsapi20170525.models.QueryShortUrlResponseBody.QueryShortUrlResponseBodyData dataBody = queryShortUrlResponseBody.getData();
        if(!ObjectVerify.isNull(dataBody)) {
            QueryShortUrlResponseModel.Data data = new QueryShortUrlResponseModel.Data();
            data.setPageViewCount(dataBody.getPageViewCount());
            data.setShortUrl(dataBody.getShortUrl());
            data.setShortUrlName(dataBody.getShortUrlName());
            data.setSourceUrl(dataBody.getSourceUrl());
            data.setUniqueVisitorCount(dataBody.getUniqueVisitorCount());

            try {
                String shortUrlStatus = dataBody.getShortUrlStatus();
                // 判断短链接状态是否为空
                if(StringVerify.isBlank(shortUrlStatus)) {
                    data.setShortUrlStatus(EnumUtil.valueOfEnum(ShortUrlStatusEnum.class,shortUrlStatus , "getCode"));
                }
                String createDate = dataBody.getCreateDate();
                // 判断创建时间是否为空
                if(StringVerify.isBlank(createDate)) {
                    data.setCreateDate(DateConvert.stringToDate(createDate, DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                }
                String expireDate = dataBody.getExpireDate();
                // 判断过期时间是否为空
                if(StringVerify.isBlank(expireDate)) {
                    data.setExpireDate(DateConvert.stringToDate(expireDate, DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                }
            } catch (BasicsException e) {
                throw new RuntimeException(e);
            }

            queryShortUrlResponseModel.setData(data);
        }

        return queryShortUrlResponseModel;


    }

    /**
     * 内容转模型
     * @param queryShortUrlResponseBody 获取短链接响应内容
     * @return 获取短链接响应模型
     */
    public static QueryShortUrlResponseModel bodyToModel(com.aliyun.sdk.service.dysmsapi20170525.models.QueryShortUrlResponseBody queryShortUrlResponseBody){
        QueryShortUrlResponseModel queryShortUrlResponseModel = new QueryShortUrlResponseModel();
        queryShortUrlResponseModel.setCode(queryShortUrlResponseBody.getCode());
        queryShortUrlResponseModel.setMessage(queryShortUrlResponseBody.getMessage());
        queryShortUrlResponseModel.setRequestId(queryShortUrlResponseBody.getRequestId());

        com.aliyun.sdk.service.dysmsapi20170525.models.QueryShortUrlResponseBody.Data dataBody = queryShortUrlResponseBody.getData();
        if(!ObjectVerify.isNull(dataBody)) {
            QueryShortUrlResponseModel.Data data = new QueryShortUrlResponseModel.Data();
            data.setPageViewCount(dataBody.getPageViewCount());
            data.setShortUrl(dataBody.getShortUrl());
            data.setShortUrlName(dataBody.getShortUrlName());
            data.setSourceUrl(dataBody.getSourceUrl());
            data.setUniqueVisitorCount(dataBody.getUniqueVisitorCount());

            try {
                String shortUrlStatus = dataBody.getShortUrlStatus();
                // 判断短链接状态是否为空
                if(StringVerify.isBlank(shortUrlStatus)) {
                    data.setShortUrlStatus(EnumUtil.valueOfEnum(ShortUrlStatusEnum.class,shortUrlStatus , "getCode"));
                }
                String createDate = dataBody.getCreateDate();
                // 判断创建时间是否为空
                if(StringVerify.isBlank(createDate)) {
                    data.setCreateDate(DateConvert.stringToDate(createDate, DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                }
                String expireDate = dataBody.getExpireDate();
                // 判断过期时间是否为空
                if(StringVerify.isBlank(expireDate)) {
                    data.setExpireDate(DateConvert.stringToDate(expireDate, DateTimeFormatEnum.yyyy_MM_dd_HH_mm_ss));
                }
            } catch (BasicsException e) {
                throw new RuntimeException(e);
            }

            queryShortUrlResponseModel.setData(data);
        }

        return queryShortUrlResponseModel;
    }

}
